import pymysql


class DBUtil:
    def __init__(self,host,user,password,port=3306):
        self.connect = pymysql.Connect(
            host=host,
            port=port,
            user=user,
            password=password,
            cursorclass=pymysql.cursors.DictCursor)  # 这个参数值得时数据库操作的结果是一个字典类型
    def select(self,sql):
        cursor=self.connect.cursor()
        cursor.execute(sql)
        data=cursor.fetchall()
        self.connect.commit()#提交事务,才能查到最新的数据
        cursor.close()
        return data
    def update(self,sql):
        cursor=self.connect.cursor()
        cursor.execute(sql)
        self.connect.commit()  # 提交事务
        cursor.close()
    def close(self):
        #关闭数据库连接对象
        if self.connect:
            self.connect.close()

if __name__ == '__main__':
    db_util=DBUtil(host='59.36.173.55',
    user='mtxshop_test',
    password='mtxshamo',)
    res=db_util.select('select * FROM mtxshop_trade.es_order where member_id = 7777918 ORDER BY order_id DESC LIMIT 2')
    print(res)
    db_util.close()


